package com.cd826dong.clouddemo.api;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.cd826dong.clouddemo.service.HystrixCommandCacheService;
import com.cd826dong.clouddemo.service.HystrixCommandFailureService;
import com.cd826dong.clouddemo.service.UserCommandCollapserService;
import com.cd826dong.clouddemo.service.UserService;
import com.netflix.hystrix.HystrixCommand.Setter;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

@RestController()
@RequestMapping("/hystrixCommandCollapserApi")
public class HystrixCommandCollapserApi {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/collapserTest")
	public String cacheTestName() throws InterruptedException, ExecutionException  {
		
		//
		
		UserCommandCollapserService collapserService1 = new UserCommandCollapserService(userService, "zhou");
		UserCommandCollapserService collapserService2 = new UserCommandCollapserService(userService, "tie");
		UserCommandCollapserService collapserService3 = new UserCommandCollapserService(userService, "qiang");
		
		Future<String> result1 = collapserService1.queue();
		Future<String> result2 = collapserService2.queue();
		//就会合并到下一次请求中
	    Thread.sleep(2000);
		Future<String> result3 = collapserService3.queue();
		
		return String.format("result1:%s,result2:%s,result2:%s", result1.get(), result2.get(), result3.get());
		
	}
}
